
#include "spserver.h"
#include "fd.h"
#include "Session.h"
#include "SessionList.h"
#include "SessionPool.h"

SessionPool::SessionPool(size_t nSize)
{
	list = NULL;
}

SessionPool* SessionPool::create(size_t nSize, size_t nRecvBufSize, size_t nSendBufSize, size_t nMaxPacketSize)
{
	SessionPool* pool = new SessionPool(nSize);
	pool->size = nSize;
	pool->list = new SessionList();

	Session *pSession = NULL;

	for (size_t i = 0; i < nSize; ++i)
	{
		pSession = new Session(nSendBufSize, nRecvBufSize, nMaxPacketSize);
		pool->list->push_back(pSession);
	}

	return pool;

}

Session* SessionPool::Alloc()
{
	list->lock();

	if (list->empty())
	{
		list->unlock();
		return NULL;
	}

	Session* pSession = list->front();
	list->pop_front();
	list->unlock();
	return pSession;
}

void SessionPool::Free(Session* pSession)
{
	list->lock();
	list->push_back(pSession);
	list->unlock();
}


